package ro.pub.cs.hwchecker.gui.client;

import ro.pub.cs.hwchecker.comm.client.data.Homework;
import ro.pub.cs.hwchecker.comm.client.json.JSONApplicationRequestBuilder;
import ro.pub.cs.hwchecker.comm.client.req.ApplicationRequestBuilder;
import ro.pub.cs.hwchecker.comm.client.req.CheckerCallback;
import ro.pub.cs.hwchecker.comm.client.req.CheckerRequest;
import ro.pub.cs.hwchecker.comm.client.req.CheckerRequestException;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class HWCheckerApp implements EntryPoint {
	public static final String APP_STATE_SUMMARY = "summary";
	public static final String APP_STATE_HOMEWORK = "homework";
	public static final String APP_STATE_GRADES = "grades";
	
	// Request performing object
	private ApplicationRequestBuilder jsonRequest = 
		new JSONApplicationRequestBuilder();
	
	// Application data
	private Homework[] homework = null;
	
	// Application interface
	HorizontalPanel appPanel;
	private MenuPane menuPane;
	
	private void handleCheckerError(CheckerRequestException ex) {
		StringBuilder sb = new StringBuilder();
		
		switch (ex.getCheckerCode()) {
		case CheckerRequestException.CODE_NOT_CONNECTED:
			break;
		default:
			sb.append("Internal application error: ");
			sb.append(ex.getCheckerCode());
			sb.append(" - ");
			sb.append(ex.getMessage());
			Window.alert(sb.toString());
			break;
		}
	}
	
	private void initHomework() throws CheckerRequestException {
		// Request for the list of homework
		jsonRequest.performHomeworkListRequest(new CheckerCallback<Homework[]>() {
		
			public void onCheckerResponse(CheckerRequest req, Homework[] hw) {
				HWCheckerApp.this.homework = hw;
		
				initApplicationGUI();
			}
		
			public void onCheckerError(CheckerRequest req, CheckerRequestException error) {
				handleCheckerError(error);
			}
		});
	}
	
	private void initApplicationData() {
		try {
			// Request homework information
			initHomework();
		} catch (CheckerRequestException e) {
			handleCheckerError(e);
		}
	}
	
	private void initApplicationGUI() {
		menuPane.updateMenuPane(homework);
		
		RootPanel.get("content").add(appPanel);
		
	}
	
	private void createApplicationGUI() {
		appPanel = new HorizontalPanel();
		
		menuPane = new MenuPane();
		
		appPanel.add(menuPane);
	}

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		// Prepare the user interface components
		createApplicationGUI();
		
		// We initialize all the application data.
		initApplicationData();
		
		// TODO: Show a visual indicator (loading stuff...)
		
		// And now we wait for the server response...
	}
  
  	public static String composeHomeworkTabState(String hwID) {
  		return APP_STATE_HOMEWORK + "/" + hwID;
  	}
  	
  	public static String composeHomeworkTabState(String hwID, String studentID) {
  		return  APP_STATE_HOMEWORK + "/" + hwID + "/" + studentID;
  	}
  	
  	public static String composeHomeworkTabState(String hwID, String studentID,
  			String archiveID) {
  		return APP_STATE_HOMEWORK + "/" + hwID + "/" + studentID + "/" +
  				archiveID;
  	}
}
